import sys

from fastapi import FastAPI
from fastapi.responses import ORJSONResponse

from common.exception import exception_handlers
from common.middleware import middlewares
from common.event import register_events
from controller import register_router


app = FastAPI(
    middleware=middlewares,
    exception_handlers=exception_handlers,
    default_response_class=ORJSONResponse,
)

register_events(app)
register_router(app)

import logging, sys

fmt = logging.Formatter(
    fmt="%(asctime)s - %(name)s:%(lineno)d - %(levelname)s - %(message)s",
    datefmt="%H:%M:%S",
)
sh = logging.StreamHandler(sys.stdout)
sh.setFormatter(fmt)

# will print debug sql
logger_tortoise = logging.getLogger("tortoise")
logger_tortoise.setLevel(logging.DEBUG)
logger_tortoise.addHandler(sh)


# uvicorn main:app --reload
if __name__ == '__main__':
    from uvicorn import run

    run("main:app", reload=True)
